# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2021年12月30日
"""
import requests
import re
import csv

url='https://movie.douban.com/top250'
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
resp = requests.get(url,headers=headers)

page_content=resp.text
obj = re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)'
                 r'</span>.*?<p class="">.*?<br>(?P<year>.*?)&nbsp.*?'
                 r'<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?'
                 r'<span>(?P<num>.*?)人评价</span>',re.S)

result=obj.finditer(page_content)
f = open('data.csv',mode='w',encoding='utf8')
csvwriter=csv.writer(f)

for i in result:
	# print(i.group('name'))
	# print(i.group('year').strip())
	# print(i.group('score'))
	# print(i.group('num'))
	dic = i.groupdict()
	dic['year']=dic['year'].strip()
	csvwriter.writerow(dic.values())

f.close()
print('Over!')
